package cn.zjyj.module.zjyj.service.riskidentifyinstance;

import cn.hutool.core.collection.CollUtil;
import cn.zjyj.module.zjyj.controller.admin.deviceinstance.vo.DeviceInstanceRespVO;
import cn.zjyj.module.zjyj.dal.dataobject.deviceinstance.DeviceInstanceDO;
import cn.zjyj.module.zjyj.dal.mysql.deviceinstance.DeviceInstanceMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.zjyj.module.zjyj.controller.admin.riskidentifyinstance.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.riskidentifyinstance.RiskIdentifyInstanceDO;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;

import cn.zjyj.module.zjyj.dal.mysql.riskidentifyinstance.RiskIdentifyInstanceMapper;

import static cn.zjyj.framework.common.common.exception.util.ServiceExceptionUtil.exception;
import static cn.zjyj.framework.common.common.util.collection.CollectionUtils.convertList;
import static cn.zjyj.module.zjyj.enums.ErrorCodeConstants.*;

/**
 * 风险辨识实例 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RiskIdentifyInstanceServiceImpl implements RiskIdentifyInstanceService {

    @Resource
    private RiskIdentifyInstanceMapper mapper;

    @Resource
    DeviceInstanceMapper deviceInstanceMapper;

    @Override
    public Long createRiskIdentifyInstance(RiskIdentifyInstanceSaveReqVO createReqVO) {
        // 插入
        RiskIdentifyInstanceDO riskIdentifyInstance = BeanUtils.toBean(createReqVO, RiskIdentifyInstanceDO.class);
        mapper.insert(riskIdentifyInstance);
        // 返回
        return riskIdentifyInstance.getId();
    }

    @Override
    public void updateRiskIdentifyInstance(RiskIdentifyInstanceSaveReqVO updateReqVO) {
        // 校验存在
        validateRiskIdentifyInstanceExists(updateReqVO.getId());
        // 更新
        RiskIdentifyInstanceDO updateObj = BeanUtils.toBean(updateReqVO, RiskIdentifyInstanceDO.class);
        mapper.updateById(updateObj);
    }

    @Override
    public void deleteRiskIdentifyInstance(Long id) {
        // 校验存在
        validateRiskIdentifyInstanceExists(id);
        // 删除
        mapper.deleteById(id);
    }

    @Override
        public void deleteRiskIdentifyInstanceListByIds(List<Long> ids) {
        // 校验存在
        validateRiskIdentifyInstanceExists(ids);
        // 删除
        mapper.deleteByIds(ids);
        }

    private void validateRiskIdentifyInstanceExists(List<Long> ids) {
        List<RiskIdentifyInstanceDO> list = mapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RISK_IDENTIFY_INSTANCE_NOT_EXISTS);
        }
    }

    private void validateRiskIdentifyInstanceExists(Long id) {
        if (mapper.selectById(id) == null) {
            throw exception(RISK_IDENTIFY_INSTANCE_NOT_EXISTS);
        }
    }

    @Override
    public RiskIdentifyInstanceDO getRiskIdentifyInstance(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public PageResult<RiskIdentifyInstanceDO> getRiskIdentifyInstancePage(RiskIdentifyInstancePageReqVO pageReqVO) {
        return mapper.selectPage(pageReqVO);
    }

    @Override
    public List<RiskIdentifyInstanceRespVO> getIdentifyInstanceWithTarget(Long identifyId) {

        List<RiskIdentifyInstanceDO> riskIdentifyInstanceDOS = mapper.selectList(RiskIdentifyInstanceDO::getIdentifyId, identifyId);
        if (CollUtil.isEmpty(riskIdentifyInstanceDOS)) {
            return new ArrayList<>();
        }

        List<Long> instanceIds = riskIdentifyInstanceDOS.stream().map(RiskIdentifyInstanceDO::getInstanceId).collect(Collectors.toList());
        List<DeviceInstanceDO> deviceInstanceDOS = deviceInstanceMapper.selectByIds(instanceIds);
        return riskIdentifyInstanceDOS.stream().map(item->{
            RiskIdentifyInstanceRespVO bean = BeanUtils.toBean(item, RiskIdentifyInstanceRespVO.class);
            deviceInstanceDOS.stream()
                    .filter(instance -> instance.getId().equals(item.getInstanceId()))
                    .findFirst()
                    .ifPresent(instance -> bean.setInstance(BeanUtils.toBean(instance, DeviceInstanceRespVO.class)));
            return bean;
        }).collect(Collectors.toList());
    }
}